#ifndef __UDSF_CSTDLIKE_C__
#define __UDSF_CSTDLIKE_C__

#include "app.h"
/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "udsf_xxx.c"形式引用
*/
#include "udsf_sys_api.c"

static void udsf_memcpy(uint8_t* src,uint8_t* dst,int len) __attribute__((unused));
static void udsf_memset(uint8_t* dst,uint8_t value,int len) __attribute__((unused));
static void udsf_memcpy32(uint32_t* src,uint32_t* dst,int len) __attribute__((unused));
static void udsf_memcpy32_hw(uint32_t*src,uint32_t*dst,int len) __attribute__((unused));

static uint32_t udsf_abs( int value) __attribute__((unused));


static uint32_t udsf_abs(int value){
	if( value >= 0 ){
		return value;
	}else{
		return (0-value);
	}
}

static int udsf_memcmp(uint8_t* src0,uint8_t* src1,int len) __attribute__((unused));
static char* udsf_strstr(const char* str1,const char* str2) __attribute__((unused));

static char* udsf_strstr(const char* str1,const char* str2)
{
	char *cp = (char *)str1;
	char *s1, *s2;

	if (!*str2)
			return((char *)str1);
	
	while (*cp)
	{
			s1 = cp;
			s2 = (char *)str2;
		
			while (*s2 && !(*s1 - *s2))
					s1++, s2++;
			
			if (!*s2)
					return(cp);
			
			cp++;
	}
	
	return(NULL);
}

static int udsf_memcmp(uint8_t* src0,uint8_t* src1,int len){
	
	int ret = 0;
	
	for( int i = 0;i<len;i++ ){
		if( src0[i] != src1[i] ){
			ret = -1;
			break;
		}
	}
	
	return ret;
}

static void udsf_memcpy(uint8_t* src,uint8_t* dst,int len)
{
	for( int i = 0;i<len;i++ ){
		dst[i] = src[i];
	}
}

static void udsf_memcpy32(uint32_t* src,uint32_t* dst,int len)
{	
	for( int i = 0;i<len;i++ ){
		dst[i] = src[i];
	}
}

static void udsf_memset(uint8_t* dst,uint8_t value,int len)
{
	for( int i = 0;i<len;i++ ){
		dst[i] = value;
	}
}

static int udsf_strlen(char* string){

	int len=0;
	for( int i=0;;i++ ){
		if( string[i] != '\0' ){
			len++;
		}else{
			break;
		}
	}
	return len;
}

static uint32_t udsf_decode_base16_be(char* str,uint8_t max_len,uint32_t max_value,int *p_status){
	
	*p_status = -1;
	uint32_t value = 0;
	for( int i = 0;i<max_len;i++){
		// if(str[i] == 0x0 ){
		// 	break;
		// }
		uint8_t data;
		if( str[i] >= '0' && str[i] <= '9'){
			data = str[i]-'0';
		}else if( str[i] >= 'a' && str[i] <= 'f' ){
			data = str[i]-'a'+10;
		}else if( str[i] >= 'A' && str[i] <= 'F' ){
			data = str[i]-'A'+10;
		}else{
			return UINT_MAX;
		}

		value <<=4;
		value |= (data&0xf);
	}
	if( value <= max_value ){
		*p_status = 0;
	}
	return value;
}
#endif
